<template>
  <div class="wrap1">
    <div class="form">
      <el-form :inline="true" :model="ruleform" ref="from" :rules="rules">
        <el-form-item label="设备名称" prop="name_like">
          <el-input
            v-model="ruleform.name_like"
            placeholder=""
            style="width: 380px"
          >
            <i
              slot="suffix"
              class="el-input__icon el-icon-circle-close"
              @click="emptyInput()"
            ></i
          ></el-input>
        </el-form-item>
        <el-form-item label="上上级分组">
          <el-select
            @change="fnOrgPrev"
            ref="prevSelect"
            @input="fnClickPrev"
            :multiple="true"
            :multiple-limit="1"
            v-model="ruleform.gfather_name"
            placeholder="请选择"
          >
            <el-option
              v-for="item in org_arr_prev"
              :key="item.gfather_name"
              :label="item.gfather_name"
              :value="item.gfather_name"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="上级分组">
          <el-select
            ref="nextSelect"
            @input="fnClickNext"
            :multiple="true"
            :multiple-limit="1"
            v-model="ruleform.father_name"
            placeholder="请选择"
          >
            <el-option
              v-for="item in org_arr_next"
              :key="item.father_name"
              :label="item.father_name"
              :value="item.father_name"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" @click="fnSearch('from')"
            >搜索</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button type="success" size="small" @click="showAll('from')"
            >全部</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <div class="map">
      <el-card>
        <Map
          v-if="childFlag"
          ref="map"
          @mark="fnMark"
          @path="fnPath"
          @getCenter="getCenter"
          :bPoints="bPoints"
          :averageCenter="true"
          :marker="marker"
          :center="center"
        ></Map>
      </el-card>
    </div>
    <el-link
      class="tips"
      :underline="false"
      type="info"
      icon="el-icon-warning-outline"
      >摄像机状态：蓝色代表在线、红色代表离线、灰色代表已取消。</el-link
    >
  </div>
</template>

<script>
import Map from "./coms/Map";
import { getData, postData } from "@/request/http";
export default {
  name: "",
  components: {
    Map,
  },
  props: {},
  data() {
    return {
      ruleform: {
        name_like: "",
        father_name: [],
        gfather_name: [],
      },
      rules: {
        name_like: [
          { required: false, message: "请输入设备名称", trigger: "blur" },
        ],
      },
      marker: [],
      center: { lng: baseCenter.lng, lat: baseCenter.lat },
      distance: 0,
      childFlag: true,
      isFirst: true,
      org_arr_prev: [],
      org_arr_next: [],
      bPoints: [],
      BMap: null,
    };
  },
  computed: {},
  created() {},
  mounted() {
    this.isFirst = true;
    this.get_gfather_name();
  },
  methods: {
    //select点击后收起
    fnClickPrev() {
      !!this.$refs.prevSelect && this.$refs.prevSelect.handleClose();
    },
    fnClickNext() {
      !!this.$refs.nextSelect && this.$refs.nextSelect.handleClose();
    },
    /*
     *@Des: 上上级分组名称
     *@Author: hhh, 2021-12-02 17:50:44
     */
    get_gfather_name() {
      postData("/adminweb/org/list/tree")
        .then((res) => {
          this.org_arr_prev =
            !res || !res.datas || !res.datas.gfathers ? [] : res.datas.gfathers;
        })
        .catch((res) => {
          this.$message.error("加载失败");
        });
    },

    /*
     *@Des: 上上级chnage
     *@Author: hhh, 2021-12-03 10:16:07
     */
    fnOrgPrev(val) {
      this.getOrgNextData(val);
      this.ruleform.father_name = "";
      if (val == "") {
        this.org_arr_next = [];
      }
    },
    /*
     *@Des: 获取下级数据
     *@Author: hhh, 2021-12-03 10:16:52
     */
    getOrgNextData(val) {
      console.log(val);
      let arr = this.org_arr_prev;
      console.log(arr);
      if (Array.isArray(arr) && arr.length > 0) {
        for (let i = 0; i < arr.length; i++) {
          let gfather_name = arr[i]["gfather_name"];
          if (gfather_name == val) {
            this.org_arr_next = !arr[i]["fathers"] ? [] : arr[i]["fathers"];
          }
        }
      }
    },
    /*
      获取中心点
    */
    getCenter(obj) {
      if (this.isFirst) {
        let { lat, lng } = obj;
        this.center = { lat, lng };
        //console.log(this.center);
        this.distance = obj.distance;
        this.submitModify();
      }
    },
    //获取中心点自动提交
    submitModify() {
      var that = this;
      //console.log(that.distance);
      that.marker = [];
      postData("/adminweb/device/list/byLocation", {
        longitude: that.center["lng"],
        latitude: that.center["lat"],
        distance: that.distance,
      })
        .then((res) => {
          //console.log(res);
          if (res.code == 0) {
            var devices = res.datas.devices;
            if (Array.isArray(devices) && devices.length > 0) {
              for (let i = 0; i < devices.length; i++) {
                that.marker.push({
                  lat: devices[i].location.latitude,
                  lng: devices[i].location.longitude,
                  ...devices[i],
                  ...devices[i].location,
                });
                 
              }
            }
          } else {
            that.$message.error(res.code + "，" + res.msg);
          }
        })
        .catch((res) => {
          that.$message.error(res.code + "，" + res.msg);
        });
    },
    //搜素
    fnSearch() {
      this.marker = [];
      this.childFlag = false;
      this.isFirst = false;
      postData("/adminweb/sys/admin/device/list", {
        name_like: this.ruleform.name_like,
        with_online: 1,
        with_location: 1,
        gfather_name: this.ruleform.gfather_name[0],
        father_name: this.ruleform.father_name[0],
      })
        .then((res) => {
          //console.log(res);
          if (res.code == 0) {
            this.marker = [];
            var devices = res.datas.devices;
            if (Array.isArray(devices) && devices.length > 0) {
              for (let i = 0; i < devices.length; i++) {
                this.marker.push({
                  lat: devices[i].location.latitude,
                  lng: devices[i].location.longitude,
                  ...devices[i],
                  ...devices[i].location,
                });
              }
            }
            this.childFlag = true;
          } else {
            this.$message.error(res.code + "，" + res.msg);
          }
        })
        .catch((res) => {
          this.$message.error("加载失败");
        });
    },
    //全部
    showAll() {
      this.submitModify();
    },
    //清空搜索框
    emptyInput() {
      this.ruleform.name_like = "";
      this.submitModify();
    },

    /*
     * @desc 地图打点
     * */
    fnMark(data) {
      this.ruleform = Object.assign({}, this.ruleform, {
        longitude: data["data"][0].lng,
        latitude: data["data"][0].lat,
        nation: "中国",
        province: data["info"]["province"],
        city: data["info"]["city"],
        district: data["info"]["district"],
        address: data["info"]["address"],
      });
    },

    /*
     * 地图路径
     * */
    fnPath(arr) {
      // console.log(arr);
    },
  },
  watch: {},
};
</script>
<style scoped>
.wrap1 {
  /* width: 100%; */
  /* height: calc(100vh - 40px); */
  width: 100%;
  height: calc(100vh - 80px);
  display: flex;
  flex-flow: column;
  flex: 1;
  overflow: hidden;
  background: #fff;
}
.wrap1 .form {
  /* display: none; */
  width: 100%;
  height: 60px;
  background: #fff;
  padding: 20px;
  box-sizing: border-box;
}
.wrap1 .map {
  flex: 1;
  width: 100%;
  overflow: hidden;
}
.wrap1 .txt {
  color: #676a6d;
  font-size: 15px;
  line-height: 40px;
  margin-right: 30px;
}
/* .btn {
  margin-left: 40px;
} */
.wrap1 >>> .tips {
  width: 100%;
  height: 40px;
  line-height: 40px;
  padding-left: 10px;
  display: block;
  font-size: 16px;
}
</style>
